package com.blank.dao.Impl;

import com.blank.dao.CheckLoginDao;
import com.blank.pojo.bo.User;
import com.blank.util.DButil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author BLANK
 * @date 2020/11/12
 */
public class CheckLoginDaoImpl implements CheckLoginDao {
    /**
     * 用户验证用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public User checkLogin(String username, String password) throws SQLException {
        DButil dButil =new DButil();
        String sql="select * from t_user where username=? and password=?";
        PreparedStatement preparedStatement=dButil.getPreparedStatement(sql);
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);
        ResultSet rs=preparedStatement.executeQuery();
        if (rs.next()){
            User user = new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setCreate_by_user(rs.getString("create_by_user"));
            user.setUpdate_by_last(rs.getDate("update_by_last"));
            user.setCreate_by_time(rs.getDate("create_by_time"));
            rs.close();
            dButil.colserResoure();
           return user;
        }else {
            rs.close();
            dButil.colserResoure();
            return null;
        }



    }

    @Override
    public User checkSameUser(String username) throws SQLException {
        DButil dButil =new DButil();
        String sql="select * from t_user where username=?";
        PreparedStatement preparedStatement=dButil.getPreparedStatement(sql);
        preparedStatement.setString(1,username);
        ResultSet rs=preparedStatement.executeQuery();
        if (rs.next()){
            User user = new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setCreate_by_user(rs.getString("create_by_user"));
            user.setUpdate_by_last(rs.getDate("update_by_last"));
            user.setCreate_by_time(rs.getDate("create_by_time"));
            rs.close();
            dButil.colserResoure();
            return user;
        }else {
            rs.close();
            dButil.colserResoure();
            return null;
        }
    }
}
